using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using umbraco.BusinessLogic.Actions;
using System.Collections.Generic;
using umbraco.interfaces;
using System.Drawing;
using umbraco.BusinessLogic;
using umbraco.BasePages;

namespace umbraco.cms.presentation.user
{

    /// <summary>
    /// An object to display the current permissions for a user and a node.
    /// </summary>
    public partial class NodePermissions : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
        }

        private User m_umbracoUser;
        private int m_nodeID = -1;
        private UserPermissions m_userPermissions;
        private string m_clientItemChecked = "void(0);";

        public int UserID
        {
            get { return m_umbracoUser.Id; }
            set
            {
                m_umbracoUser = BusinessLogic.User.GetUser(value);
                m_userPermissions = new UserPermissions(m_umbracoUser);
            }
        }

        public int NodeID
        {
            get { return m_nodeID; }
            set { m_nodeID = value; }
        }

        /// <summary>
        /// The JavaScript method to call when a node is checked. The method will receive a comma seperated list of node IDs that are checked.
        /// </summary>
        public string OnClientItemChecked
        {
            get { return m_clientItemChecked; }
            set { m_clientItemChecked = value; }
        }

        public override void DataBind()
        {
            base.DataBind();
            
            lblMessage.Visible = false;

            if (m_umbracoUser == null)
                throw new ArgumentNullException("No User specified");

            //get the logged in user's permissions
            UserPermissions currUserPermissions = new UserPermissions(UmbracoEnsuredPage.CurrentUser);
            List<IAction> lstCurrUserActions = currUserPermissions.GetExistingNodePermission(m_nodeID);

            List<IAction> lstLookupUserActions = m_userPermissions.GetExistingNodePermission(m_nodeID);
            List<IAction> lstAllActions = Action.GetPermissionAssignable();

            //no node is selected, disable the check boxes
            if (m_nodeID == -1)
            {
                ShowMessage("No node selected");
                return;
            }

            //ensure the current user has access to assign permissions.
            //if their actions list is null then it means that the node is not published.
            if (lstCurrUserActions == null || lstCurrUserActions.Contains(ActionRights.Instance))
                BindExistingPermissions(lstAllActions, lstLookupUserActions);
            else
                ShowMessage("You do not have access to assign permissions to this node");

            //bind the div with the comma seperated values of the letters
            if (lstLookupUserActions != null)
                foreach (IAction a in lstLookupUserActions)
                    hidCurrPermissions.InnerText += a.Letter.ToString() + ",";
            hidCurrPermissions.InnerText = hidCurrPermissions.InnerText.TrimEnd(new char[] { ',' });

            businesslogic.CMSNode node = new umbraco.cms.businesslogic.CMSNode(m_nodeID);
            lblNodeName.Text = node.Text;
        }

        private void ShowMessage(string msg)
        {
            lblMessage.Visible = true;
            lblMessage.Text = msg;
            pnlPermissionList.Visible = false;
        }

        private void BindExistingPermissions(List<IAction> allActions, List<IAction> userActions)
        {
            List<AssignedPermission> assignedPermissions = new List<AssignedPermission>();
            foreach (umbraco.interfaces.IAction a in allActions)
            {
                AssignedPermission p = new AssignedPermission();
                p.Permission = a;
                p.HasPermission = (userActions != null ? userActions.Contains(a) : false);
                assignedPermissions.Add(p);
            }
            rptPermissionsList.DataSource = assignedPermissions;
            rptPermissionsList.DataBind();
        }        

        protected struct AssignedPermission
        {
            public IAction Permission;
            public bool HasPermission;
        }

    }
}